# coding:utf-8
import os
import sqlite3
import sys
import traceback

from bs4 import BeautifulSoup
from vip.id_encode import GetLngid
from xjlibrary.mdatetime.mtime2 import MDateTimeUtils
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -3)
sPath = BaseDir.get_new_path(TopPath, "download", "zswj", 'download', "Detail")
coverpath = BaseDir.get_new_path(TopPath, "download", "zswj", 'download', "cover")
sGUrl = r'http://www.libvideo.com/danpian/danpian.aspx?id='
ISOTIMEFORMAT = '%Y%m%d'
sqlList = list()
nCount = 0
nCover = 0

CountryDict = {'新西兰': 'NZ', '荷兰': 'NL', '瑞典': 'SE', '加拿大': 'CA', '西班牙': 'ES', '奥地利': 'AT', '意大利': 'IT', '中国': 'CN',
               '英国': 'UK', '日本': 'JP', '美国': 'US', '法国': 'FR', '德国': 'DE', '韩国': 'KR', '澳大利亚': 'AU', '国际': 'UN',
               '捷克': 'CZ'}
LanguageDict = {'NZ': 'EN', 'NL': 'NL', 'SE': 'SV', 'CA': 'EN', 'ES': 'ES', 'AT': 'EN', 'IT': 'IT', 'CN': 'ZH',
                'UK': 'EN', 'US': 'EN', 'JP': 'JA', 'FR': 'FR', 'DE': 'DE', 'UN': 'UN', 'KR': 'KO', 'AU': 'EN',
                'CZ': 'CS', }


def output(value, files='log.txt'):
    f = open(files, 'w', encoding='utf-8')
    f.write(value)
    f.close()


# 转移字符
def sqliteEscape(keyWord):
    keyWord = keyWord.replace('/', '//')
    keyWord = keyWord.replace("'", "''")
    keyWord = keyWord.replace("[", "/[")
    keyWord = keyWord.replace("]", "/]")
    keyWord = keyWord.replace("%", "/%")
    keyWord = keyWord.replace("&", "/&")
    keyWord = keyWord.replace("_", "/_")
    keyWord = keyWord.replace("(", "/(")
    keyWord = keyWord.replace(")", "/)")

    return keyWord


# 插入数据库
def InsertIntoDb():
    global sqlList, nCount
    dbMsg = None
    # conn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s' %os.path.join(curPath,'wanfang_bz.accdb'))
    conn = sqlite3.connect('libvideo.db3')
    cur = conn.cursor()
    if conn:
        for sql in sqlList:
            nCount += 1
            print('Done:' + str(nCount))
            try:

                cur.execute(sql)
            except:
                dbMsg = '*InsertError:' + traceback.format_exc()
            if dbMsg:
                print(dbMsg)
                continue
            # exit(-1)
    conn.commit()
    conn.close()
    sqlList = list()


def AnalysisFile(filePath, fileName):
    global sqlList, nCover
    title = ''
    country = ''
    catalog = ''
    timespan = ''
    description = ''
    cover = ''

    soup = BeautifulSoup(open(filePath, mode='r', encoding='utf-8'), 'lxml')
    title = ''.join(soup.find('span', id='labTitle').stripped_strings)
    # ---------------------------------------
    vid = fileName.replace('.htm', '')

    # ---------------------------------------
    countryTag = soup.find('span', id='labFrom')
    if not countryTag:
        countrys = '国际'
    else:
        countrys = ''.join(countryTag.stripped_strings)
        if countrys == "" or countrys == "Label" or countrys == "不详":
            countrys = '国际'
    country = CountryDict.get(countrys)
    language = LanguageDict.get(country, 'EN')
    # ----------------------------------------------
    catalogTag = soup.find('span', id='labclasstitle')
    if catalogTag:
        catalog = ''.join(catalogTag.stripped_strings)
    # --------------------------------------------------
    timeSpanTag = soup.find('span', id='labLongTime')
    if timeSpanTag:
        timespan = ''.join(timeSpanTag.stripped_strings)
    # --------------------------------------------------
    descriptionTag = soup.find('span', id='labContent')
    if descriptionTag:
        description = ''.join(descriptionTag.stripped_strings)

    title = sqliteEscape(title)
    description = sqliteEscape(description)
    year = '1900'
    pubdate = '19000000'
    type_ = '10'
    sub_db_id = "00153"
    product = "LIBVIDEO"
    sub_db = "DMT"
    provider_zt = "libvideo"
    provider = "YUANLAI"

    provider_id = provider_zt + '@' + vid
    provider_url = provider_zt + '@' + sGUrl + vid
    # lngid = 'LIBVIDEO_DMT_' + vid

    lngid = GetLngid(sub_db_id, vid)
    coverPath = os.path.join(coverpath, vid + '.jpg')
    if os.path.exists(coverPath):
        cover = '/smartlib/' + provider_zt + '/' + vid + '.jpg'
    else:
        print('cover not exists')
        nCover += 1
    medium = "2"
    publisher = "武汉缘来文化传播有限责任公司"
    batch = MDateTimeUtils.get_today_date_strings() + "00"
    sql = "INSERT INTO modify_title_info_zt(lngid, rawid, title, \
		date, description, date_created, language, country, type, provider, provider_id, provider_url, provider_subject, folio_size, cover,\
		medium,batch,publisher) VALUES('{lngid}', '{rawid}', '{title}', '{date}', '{description}', \
		'{date_created}', '{language}', '{country}', '{type}', '{provider}', '{provider_id}', '{provider_url}', \
		'{provider_subject}', '{folio_size}', '{cover}','{medium}','{batch}','{publisher}')".format(lngid=lngid,
                                                                                                    rawid=vid,
                                                                                                    title=title,
                                                                                                    date=year,
                                                                                                    description=description,
                                                                                                    date_created=pubdate,
                                                                                                    language=language,
                                                                                                    country=country,
                                                                                                    type=type_,
                                                                                                    provider=provider_zt,
                                                                                                    provider_id=provider_id,
                                                                                                    provider_url=provider_url,
                                                                                                    provider_subject=catalog,
                                                                                                    folio_size=timespan,
                                                                                                    cover=cover,
                                                                                                    medium=medium,
                                                                                                    batch=batch,
                                                                                                    publisher=publisher)
    sqlList.append(sql)

    if len(sqlList) > 100:
        InsertIntoDb()


# ---------------------------------作业开始---------------------------------
def main():
    if not os.path.exists(sPath):
        print('目录不存在:' + sPath)
        sys.exit(-1)
    for path, dirNames, fileNames in os.walk(sPath):
        for fileName in fileNames:
            filePath = os.path.join(path, fileName)
            print(filePath)
            AnalysisFile(filePath, fileName)
    if len(sqlList) > 0:
        InsertIntoDb()

    print('No FilePath Cover:%d' % nCover)


if __name__ == '__main__':
    main()